package com.bw.spsy.pay.order;
import com.alibaba.fastjson2.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: liuzhiping
 * @date: 2025/03/21 15:50
 **/
public class WeiXinUtil {

    //订单号
    public static final String ORDER_ID = "3724200166688801787";

    /***
     * 下单接口
     * @return
     */
    public static String payOrder(String orderId) throws  Exception{
        Map<String,String> map = new HashMap<String,String>();

        map.put("appid",Constants.APP_ID);
        map.put("mch_id",Constants.MCH_ID);



        map.put("nonce_str",CommUtil.getNonce_str());


        map.put("body","专高6-测试商品-【武雪敏 0523 都是发的】同学创建");
        //map.put("out_trade_no",WeiXinUtil.ORDER_ID);

        map.put("out_trade_no",orderId);

        map.put("total_fee","2");
        map.put("spbill_create_ip","127.0.0.1");



        map.put("notify_url",Constants.NOTIFY_URL);




        map.put("trade_type","NATIVE");

        map.put("sign_type","HMAC-SHA256");


        //map.put("sign",CommUtil.generateSignature(map, Constants.API_KEY, "HMACSHA256"));


        //自己写的签名算法
        map.put("sign",CommUtil2210a.generateSignature(map, Constants.API_KEY, "HMACSHA256"));



        String reqXml = XMLUtil.mapToXml(map);


        String respXml = HttpUtil.postData(Constants.UFDODER_URL,reqXml);

        System.out.println(respXml);



        Map<String,String> respMap = XMLUtil.doXMLParse(respXml);

        String codeUrl = respMap.get("code_url");


        //

        System.out.println("------------------>....");
        System.out.println(codeUrl);
        System.out.println("------------------>....");

        return codeUrl;

    }

    /**
     *
     * @return
     */
    public static String queryOrder(String orderId) throws Exception{


        //String orderId = WeiXinUtil.ORDER_ID;



        Map<String,String> map = new HashMap<String,String>();

        map.put("appid",Constants.APP_ID);
        map.put("mch_id",Constants.MCH_ID);
        map.put("nonce_str",CommUtil.getNonce_str());
        map.put("out_trade_no",orderId);

        map.put("trade_type","NATIVE");

        map.put("sign_type","HMAC-SHA256");


        //原来微信生成的sign
        //map.put("sign",CommUtil.generateSignature(map, Constants.API_KEY, "HMACSHA256"));

        //自己写的签名算法
        map.put("sign",CommUtil2210a.generateSignature(map, Constants.API_KEY, "HMACSHA256"));


        String reqXml = XMLUtil.mapToXml(map);


        String respXml = HttpUtil.postData(Constants.CHECK_URL,reqXml);
        System.out.println(respXml);

        Map<String,String> respMap = XMLUtil.doXMLParse(respXml);

        return JSON.toJSONString(respMap);
    }


    public static void main(String[] args) throws  Exception {
        WeiXinUtil.payOrder(WeiXinUtil.ORDER_ID);
        //WeiXinUtil.queryOrder(WeiXinUtil.ORDER_ID);
    }
}
